package com.etc.admin.controller;/*
 * 文件名：com.etc.admin.controller.EmployeePersonalContrlooer
 * 描述：后台员工修改个人信息、录入人脸的控制器
 * 作者：黄志鹏
 * 时间：2019/5/26 19:28
 */

import com.alibaba.fastjson.JSONObject;
import com.etc.service.EmployeePersonalService;
import com.etc.service.EmployeeService;
import com.etc.utils.AddFace;
import com.etc.utils.CommonMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("admin.action")
public class EmployeePersonalController {

    //注入员工个人操作的service
    @Autowired
    private EmployeePersonalService employeePersonalService;

    /**
     * 用于员工修改个人资料
     * @param empname 员工名
     * @param emppassword 密码
     * @param empsex 性别
     * @return 返回信息
     */
    @RequestMapping(value = "updateEmployeePersonalData",method = RequestMethod.PUT)
    @ResponseBody
    public CommonMessage updateEmployeePersonalData(String empname,String emppassword,String empsex ,Integer empid){
        //创建一个返回值对象
        CommonMessage commonMessage = new CommonMessage();

        //调用service层修改员工信息
        boolean flag =employeePersonalService.updateEmployee(empname,emppassword,empsex,empid);

        if (flag){
            //修改成功设置返回值
            commonMessage.setMsg("修改成功");
        }
        else {
            //修改失败，设置返回值信息
            commonMessage.setMsg("修改失败");
        }
        return commonMessage;
    }

    /**
     * 跳转至人脸录入界面使用的方法
     * @return
     */
    @RequestMapping("face")
    public String toFace(){
        return "admin/face";
    }

    /**
     * 人脸录入的方法
     * @param image 要录入的人脸信息
     * @param empid 录入的员工id
     * @return 录入是否成功
     */
    @RequestMapping(value = "entryFace")
    @ResponseBody
    public CommonMessage entryface(String image, Integer empid) {
        CommonMessage cm = new CommonMessage();

        //System.out.println(image);
        String imgStr = image;
        //System.out.println(empid);
        if (null != imgStr) {
            imgStr = imgStr.substring(imgStr.indexOf(",") + 1);
        }
        // Boolean flag = GenerateImage(imgStr, filePath, fileName);
        String result = AddFace.add(imgStr, empid + "");
        JSONObject jo = JSONObject.parseObject(result);
        String str = (String) jo.get("error_msg");
        if (str.equals("SUCCESS")) {
            cm.setMsg("录入成功");
            // response.getWriter().println("新增成功");
        } else {
            cm.setMsg("录入失败");
            // response.getWriter().println("新增失败");
        }
        return cm;
    }
}
